<?php

namespace app\api\controller;

use app\admin\model\commission\Log;
use app\admin\model\commission\Rate;
use app\common\controller\Api;
use app\common\model\MoneyLog;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public static function index($param)
    {
        $log=new Log();
        $data=[
            "user_id"=>$param['user_id'],//获得者ID
            "order_id"=>$param['order_id'],//订单ID
            "order_user_id"=>$param['order_user_id'],//订单的购买者ID
            "role"=>$param['role'],//获得者身份
            "money"=>$param['money'],//获得金额
            "rate"=>$param['rate'],//金额比例
            "order_money"=>$param['order_money'],//订单总金额
            "type"=>$param['type'],//类型
        ];
        $log->addLog($data);
    }

    public function login()
    {
        $this->auth->direct(85);
        $this->success('登录成功', ['token'=>$this->auth->getToken(),'user'=>$this->auth->getUserinfo()]);
    }


    //金额操作
    public static function op_money($userid, $money, $op,$type='',$memo='',$title='',$orderid)
    {
        $User = \app\admin\model\User::where('id', '=', $userid)->find();
        if ($op == 'dec') {
            if ($User['money'] - $money < 0) {
                fail('余额不足');
            }
            $y_money=$User['money']-$money;
            $result= \app\admin\model\User::where('id', '=', $userid)->where('money','egt',$money)->update(['money'=>$y_money]);
        } else {
            $y_money=$User['money']+$money;
            $result= \app\admin\model\User::where('id', '=', $userid)->update(['money'=>$y_money]);
        }
        if($result>0){
            MoneyLog::create(['user_id'=>$userid,'money'=>$money,'before'=>$User['money'],'after'=>$y_money,'memo'=>$memo,'type'=>$type,'title'=>$title,'order_id'=>$orderid]);
        }
    }

}
